﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Security;
using System.Web.ClientServices;
using System.Data.Entity;
using Top.Api.Domain;
using System.Text.RegularExpressions;

namespace AscentDesktop
{
    public partial class DashboardForm : Form
    {
        public DashboardForm()
        {
            InitializeComponent();
        }

        private void DashboardForm_Load(object sender, EventArgs e)
        {
            //if (!Membership.ValidateUser("admin", "jie0208?"))
            //{
            //    MessageBox.Show("Unable to authenticate.", "Not logged in",
            //        MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    Application.Exit();
            //}
            //MessageBox.Show("Welcome to the Client Application Services Demo.", "Welcome!");

            //if (!ValidateUsingCredentialsProvider()) return;
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TopContext>());
        }

        /// <summary>
        /// http://msdn.microsoft.com/zh-cn/library/bb546195.aspx
        /// </summary>
        /// <returns></returns>
        private bool ValidateUsingCredentialsProvider()
        {
            bool isAuthorized = false;
            try
            {
                // Call ValidateUser with empty strings in order to display the 
                // login dialog box configured as a credentials provider.
                isAuthorized = Membership.ValidateUser(String.Empty, String.Empty);
            }
            catch (System.Net.WebException)
            {
                if (DialogResult.OK == MessageBox.Show("Unable to access the authentication service." + Environment.NewLine + "Attempt login in offline mode?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
                {
                    ConnectivityStatus.IsOffline = true;
                    isAuthorized = Membership.ValidateUser(String.Empty, String.Empty);
                }
            }

            if (!isAuthorized)
            {
                MessageBox.Show("Unable to authenticate.", "Not logged in", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            return isAuthorized;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var db = new TopContext();
            var item = new Item();
            item.NumIid = 57711100;
            item.Title = "workjie";
            item.Location = new global::Top.Api.Domain.Location() { Country = "china" };
            db.Items.Add(item);
            db.SaveChanges();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var db = new TopContext();
            var item = (from o in db.Items
                        where o.NumIid == 57711100
                        select o).First();

            
            item.Title = "workgang";
            item.Location = new global::Top.Api.Domain.Location() { Country = "china" };      
            
            db.SaveChanges();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string nicks = "胖九点com,胖丁mami,小虫米子,轻熟女掌柜,miumiu宝贝,蓝尾小鱼,upin365,等待二月";
            List<string> x = nicks.Split(',').Take(5).ToList<string>();
            List<string> y = new List<string>();
            y.AddRange(x);
            string z = y.ToString();
        }
    }

    public class TopContext : DbContext
    {
        
        public DbSet<Item> Items { get; set; }
    }
}
